import {
	pagesIndex
} from "@/config"
import {
	objectToQueryString,
	getQueryParams
} from "@/utils/url.js"
export function getObjToQueryStrDecode(params = {}) {
	return decodeURIComponent(objectToQueryString(params))
}
export function backPage(params={}){
	var pages=getPages()
	if(pages.length>0){
		uni.navigateBack()
	}else{
		initPage(params)
	}
}
export function loginPage(params = {}) {
	var queryString = getObjToQueryStrDecode(params)

	uni.navigateTo({
		url: "/pages/login/index?" + queryString
	})
}
export function regPage(params = {}) {
	var queryString = getObjToQueryStrDecode(params)
	uni.navigateTo({
		url: "/pages/login/reg?" + queryString
	})
}
export function indexPage(params = {}) {
	var queryString = getObjToQueryStrDecode(params)
	uni.navigateTo({
		url: pagesIndex + "?" + queryString
	})
}
// 获取路由页面
export async function getPages(num=1) {
	return new Promise((resolve, reject) => {

		// 当应用显示时执行
		let pages = getCurrentPages();
	
		// console.log(JSON.stringify(pages))
		if (pages[0]) {
			resolve(pages)
			return
		}
		if(num==5){
			console.log(num)
			return
		}
		setTimeout( async () => {
			num++
		var page1=getPages(num)
		page1.then(e=>{
			resolve(e)
		})
		// console.log(page1)
		// 	resolve(page1)
		}, 300)
	})
}
// 去首页，如果url参数有page=/pages/ 页面参数，会跳转到该页面
export function initPage(params = {}) {
	var page = pagesIndex
	if (params.page) {
		page = params.page
		delete params.page
	}
	var queryString = objectToQueryString(params)
	var lianjie = "?"
	if (page.indexOf("?") > -1) {
		var queryParams = getQueryParams(page)
		queryString = objectToQueryString({
			...params,
			...queryParams
		})
		page = page.substring(0, page.indexOf("?"))
	}
	
	var url = page + lianjie + queryString
	var url = decodeURIComponent(url)
	uni.navigateTo({
		url
	})
}